<template>
  <div class="order">
    <van-nav-bar title="我的订单" left-arrow @click-left="$router.go(-1)"/>
    <van-tabs @change="onChange" v-model="name">
      <van-tab title="全部" name="all"></van-tab>
      <van-tab title="待支付" name="payment"></van-tab>
      <van-tab title="待发货" name="delivery"></van-tab>
      <van-tab title="待收货" name="received"></van-tab>
      <van-tab title="待评价" name="comment"></van-tab>
    </van-tabs>

    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <!--        <van-cell v-for="item in list" :key="item" :title="item"/>-->
        <OrderListItem v-for="order in list" :order="order" :type="title" :key="order.order_id"></OrderListItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import OrderListItem from '@/components/OrderListItem'
import { orderList } from '@/api/order'

export default {
  name: 'OrderPage',
  components: {
    OrderListItem
  },
  data () {
    return {
      title: '全部',
      name: this.$route.query.dataType || 'all',
      list: [],
      currentPage: 1,
      perPage: 0,
      lastPage: 0,
      loading: true,
      finished: false,
      refreshing: false
    }
  },
  methods: {
    async onLoad () {
      console.log('onLoad')
      console.log(`refreshing = ${this.refreshing}`)
      if (this.refreshing) {
        this.currentPage = 1
        this.refreshing = false
      }
      await this.switchOrderStatus(this.name, this.title)
      this.loading = false
      console.log(`currentPage = ${this.currentPage}`)
      if (this.currentPage >= this.lastPage) {
        console.log('请求完成')
        this.finished = true
      }
      this.currentPage++
    },
    onRefresh () {
      console.log('onRefresh')
      // 清空列表数据
      this.finished = false
      this.list = []
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    async onChange (name, title) {
      console.log('change')
      this.currentPage = 1
      this.list = []
      this.title = title
      this.name = name
      await this.switchOrderStatus(name, title)
    },
    async loadOrderList ({
      dataType = 'all',
      page = 1
    } = {}) {
      const {
        list: {
          current_page: currentPage,
          per_page: perPage,
          last_page: lastPage,
          data
        }
      } = await orderList({
        dataType,
        page
      })
      this.currentPage = currentPage
      this.perPage = perPage
      this.lastPage = lastPage
      // this.list = data
      this.list.push(...data)
      console.log(this.list)
    },
    async switchOrderStatus (name, title) {
      console.log('switchOrderStatus')
      await this.loadOrderList({ dataType: name, page: this.currentPage })
    }
  },
  created () {
    this.onLoad()
  }
}
</script>

<style lang="less" scoped>
.order {
  background-color: #fafafa;
}

.van-tabs {
  position: sticky;
  top: 0;
}
</style>
